// miniprogram/pages/test/test.js
import Approval from "../../core/Approval.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projects: getApp().globalData.projects,
    selectProject: getApp().globalData.nowPrj,
    current: 'check',
    parameter: [{ id: 0, name: '待批复', state: "now" }, { id: 1, name: '批复历史', state: "finish" }],
    selectParameter: 0,

    //shenpi
    approvalList: null,
    approvalListCopy: null,
    loading: true,
    currentTab: 0, //预设当前项的值

    //


    current_scroll: 1,
    open: false,
    // mark 是指原点x轴坐标
    markX: 0,
    // newmark 是指移动的最新点的x轴坐标 
    newmarkX: 0,
    markY: 0,
    newmarkY: 0,
    istoright: true,

    winHeight: "",//窗口高度

    scrollLeft: 0, //tab标题的滚动条位置

    //抽屉
    hiddenDataPicker: true,
    selectApproval: null,
    TunnelDrawerIndex: 0,
    swiperIndex: 0,
    indicatorDots: true,  //小点
    autoplay: true,  //是否自动轮播
    interval: 300000,  //间隔时间
    duration: 300,  //滑动时间
    circular: true,
    Result_list:["同意","驳回"],
    selectProcess:null,
  },
  //刷新
  onRefresh() {
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.init();
  },
  init(){
    this.data.parameter[0].checked = true;
    this.setData({
      parameter: this.data.parameter,
      selectParameter: 0
    })//默认parameter数组的第一个对象是选中的
    //---------------------------------------
    this.setData({
      projects: getApp().globalData.projects,
      selectProject: getApp().globalData.nowPrj,
    })

    console.log(this.data.selectProject);
    var that = this;
    // 高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    this.initData();
  },
  onLoad: function () {
   this.init();
  },
  bindApproval_Opinion(e){
    let selectProcess = this.data.selectProcess;
    selectProcess.Opinion = e.detail.value;
    this.setData({ selectProcess: selectProcess });
  },
  resultChangedClick(e){
    let selectProcess = this.data.selectProcess;
    selectProcess.Result = this.data.Result_list[e.detail.value];
    this.setData({
      selectProcess: selectProcess
    })
  },

  sureClick() {
    console.log(this.data.selectProcess);
    this.data.selectProcess.modify()
    .then(
      res=>{
        this.setData({
          hiddenDataPicker: true
        })
        this.onRefresh();
      }
    )
  },

  TunnelDrawerClick(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      TunnelDrawerIndex: index
    })
  },
  viewA(e) {

    console.log(e);
    let item = new Approval(e.currentTarget.dataset.vitem);
    console.log(item);

    let selectProcess = item.approvalProcesses.find(x => x.Ref_Person_Guid == getApp().globalData.user.personnelInfo.GuidID);
    console.log(selectProcess)
    item.getImgs()
      .then(
        res => {
          this.setData({
            selectApproval: item,
            hiddenDataPicker: false,
            selectProcess: selectProcess
          })

        }
      )

  },
  previewImg(e) {
    let index = e.target.dataset.index;
    wx.previewImage({
      current: e.target.dataset.list[index],
      urls: e.target.dataset.list,
    })
  },
  // 参数点击响应事件
  parameterTap: function (e) {//e是获取e.currentTarget.dataset.id所以是必备的，跟前端的data-id获取的方式差不多
    var that = this
    var this_checked = e.currentTarget.dataset.id
    var parameterList = this.data.parameter//获取Json数组
    for (var i = 0; i < parameterList.length; i++) {
      if (parameterList[i].id == this_checked) {
        parameterList[i].checked = true;//当前点击的位置为true即选中
      }
      else {
        parameterList[i].checked = false;//其他的位置为false
      }
    }
    that.setData({
      parameter: parameterList,
      selectParameter: this_checked
    })
    this.updateData();
  },
  initData() {
    Approval.loadList_other()
      .then(
        res => {
          console.log(res);
          res.forEach(x => {
            let find = getApp().globalData.sys.ruletemplate_list.find(y => y.GuidID == x.ApprovalRule)
            if (find) {
              x.ApprovalRuleName = find.Remark;
            }
          })
          this.setData({
            approvalList: res
          })
          this.updateData();
        }
      )
  },
  updateData() {
    this.setData({
      loading: true,
    })
    let approvalListCopy = [];
    let value = null;
    switch (this.data.currentTab) {
      case 0: value = "合同审批"; break;
      case 1: value = "支付审批"; break;
      case 2: value = "事项审批"; break;
      default: break;
    }
    for (let i = 0; i < this.data.approvalList.length; i++) {
      if ((this.data.approvalList[i].userState == this.data.parameter[this.data.selectParameter].state) &&(this.data.approvalList[i].Approval_Type_Guid == value)) {
        approvalListCopy.push(this.data.approvalList[i]);
      }
    }
    wx.nextTick(() => {
      this.setData({
        loading: false,
        approvalListCopy: approvalListCopy
      })
      console.log(this.data.approvalListCopy)

      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    })
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    this.updateData();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
      this.updateData();
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  }
})